package com.mz.demo.handler;

import com.alibaba.fastjson.JSON;
import com.mz.demo.utils.entity.GlobalException;
import com.mz.demo.utils.entity.R;
import com.mz.demo.utils.tools.WebUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 *  spring security 认证失败处理
 * */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {

        WebUtils.renderString(response, JSON.toJSONString(R.fail(401, "认证失败,请重新登录")));
    }
}
